package com.example.allshengmedical.welcome.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.allsheng.widget.AllShengDialog;
import com.allsheng.widget.AllShengDialog.ClickListenerInterface;
import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseActivity;
import com.example.allshengmedical.mainmenu.activity.MainMenuActivity;
import com.example.allshengmedical.util.ByAlert;
import com.example.allshengmedical.util.MyLog;
import com.example.allshengmedical.welcome.presenter.WelcomPresenter;
import com.example.allshengmedical.welcome.view.IWelcomeView;
import com.itboye.allshengmedical.sci.DefaultSerialControl;
import com.itboye.allshengmedical.sci.ISerialControl;
import com.itboye.allshengmedical.sci.cmd.req.CmdType;
import com.itboye.allshengmedical.sci.packet.resp.BaseRespPacket;
import com.itboye.allshengmedical.sci.packet.resp.SelfCheckRespPacket;

public class WelcomeActivity extends BaseActivity implements IWelcomeView {

	ImageView bigCircle, smallCircle;
	WelcomPresenter welcomPresenter;
	// 端口
	private ISerialControl controlInstance;
	SharedPreferences preferences;
	Editor editor;
	boolean isRun = true;

	@Override
	protected int setcontent() {
		return R.layout.activity_welcome;
	}

	@Override
	protected void initView() {
		preferences = getSharedPreferences("SelfCheckFail", MODE_PRIVATE);
		editor = preferences.edit();
		editor.putBoolean("SelfCheckFail", true);
		editor.putString("SelfCheckDetail", "");
		editor.commit();
		controlInstance = DefaultSerialControl.getInstance();
		controlInstance.init();
		controlInstance.setHandler(mHandler);
		welcomPresenter = new WelcomPresenter(this, bigCircle, smallCircle,
				this);
		welcomPresenter.spin();
		if (controlInstance == null) {
			ByAlert.alert(getString(R.string.welcome_duankou));
			return;
		} else {
			controlInstance.init();
			controlInstance.start();
			controlInstance.addCommand(CmdType.SelfCheck, null, 0);
		}

	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case ISerialControl.FindPacketMessage:
				// 完成主界面更新,拿到数据
				update(msg.getData());
				break;
			case ISerialControl.SCTimeoutMessage:
				// ByAlert.alert("自检请求超时,请重试");
				final AllShengDialog allShengDialog = new AllShengDialog(
						WelcomeActivity.this, "", "自检请求超时", "OK");
				allShengDialog.show();
				controlInstance.pause();
				allShengDialog.setClicklistener(new ClickListenerInterface() {

					@Override
					public void OK() {
						// TODO Auto-generated method stub
						Intent intent2 = new Intent(WelcomeActivity.this,
								MainMenuActivity.class);
						isRun = false;
						editor.putBoolean("SelfCheckFail", isRun);
						editor.putString("SelfCheckDetail", "自检请求超时");
						editor.commit();
						startActivity(intent2);
						finish();
					}

					@Override
					public void Cancle() {
						// TODO Auto-generated method stub
						allShengDialog.dismiss();

					}
				});

			default:
				break;
			}

		};
	};

	protected void update(Bundle bundle) {
		// TODO Auto-generated method stub
		String cmdType = bundle.getString("cmdType");
		BaseRespPacket packet = (BaseRespPacket) bundle
				.getSerializable("packet");
		if (cmdType.length() < 1)
			return;
		// 根据type 转换packet 为你想要的
		CmdType type = CmdType.getCmdType(cmdType.substring(1));
		// 发送指令后这里会收到数据
		Log.d("read_data", "接受到的响应数据包是" + cmdType + type.toDesc());
		switch (type) {

		case SelfCheck:
			// 转换成自检数据包
			final SelfCheckRespPacket selfCheckRespPacket = new SelfCheckRespPacket(
					packet);
			if (packet != null) {
				if (selfCheckRespPacket.isSuccess()) {
					Intent intent = new Intent(this, MainMenuActivity.class);
					this.startActivity(intent);
					this.finish();
					controlInstance.pause();
				} else {
					final AllShengDialog allShengDialog = new AllShengDialog(
							this, "自检请求错误信息",
							selfCheckRespPacket.getErrorDesc(), "OK");

					allShengDialog.show();
					controlInstance.pause();
					allShengDialog
							.setClicklistener(new ClickListenerInterface() {

								@Override
								public void OK() {
									// TODO Auto-generated method stub
									Intent intent = new Intent(
											WelcomeActivity.this,
											MainMenuActivity.class);
									intent.putExtra("isRun", false);
									isRun = false;
									editor.putBoolean("SelfCheckFail", isRun);
									editor.putString("SelfCheckDetail",
											selfCheckRespPacket.getErrorDesc());
									editor.commit();
									startActivity(intent);
									finish();
								}

								@Override
								public void Cancle() {
									// TODO Auto-generated method stub
									allShengDialog.dismiss();
								}
							});

				}
				// Toast.makeText(BaseApplication.getContext(),
				// "【自检错误信息】" + selfCheckRespPacket.getErrorDesc(),
				// Toast.LENGTH_LONG).show();
				// Log.e("hu...self", selfCheckRespPacket.getErrorDesc());
				// Log.e("hu...self2", selfCheckRespPacket.ModeOneTemp());
			}
			break;
		}
	}

	@Override
	protected void initData() {

	}

	@Override
	public void spin(ImageView bigView, ImageView smallView) {
		// ByAlert.alert("Welcome");
		/** ������ת���� */
		final RotateAnimation animation1 = new RotateAnimation(0f, 360f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);

		final RotateAnimation animation2 = new RotateAnimation(360f, 0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);

		animation1.setInterpolator(new LinearInterpolator());// ��ͣ��
		animation1.setDuration(3500);
		animation1.setRepeatCount(-1);
		bigView.setAnimation(animation1);
		animation1.startNow();

		animation2.setInterpolator(new LinearInterpolator());// ��ͣ��
		animation2.setDuration(2500);
		animation2.setRepeatCount(-1);
		smallView.setAnimation(animation2);
		animation2.startNow();

	}

}
